#include "util/tc_common.h"
#include "util/tc_thread.h"
#include "util/tc_sem_mutex.h"
#include "jmem/jmem_queue.h"
#include "Test.h"

using namespace std;
using namespace taf;

JceQueue<Test::QueueElement, SemLockPolicy, FileStorePolicy> g_queue;

class PushThread : public TC_Thread
{
    protected:
        virtual void run()
        {
            Test::QueueElement qe;
            while (true)
            {
                bool b;
                qe.s = TC_Common::nowtime2str();
                b = g_queue.push_back(qe);

                if (!b)
                {
                    cout << "push full" << endl;
                    sleep(1);
                    continue;
                }
                qe.i++;
            }
        }
};

class PopThread : public TC_Thread
{
    protected:
        virtual void run()
        {
            Test::QueueElement qe;
            while (true)
            {
                bool b;
                b = g_queue.pop_front(qe);

                if (!b)
                {
                    cout << "pop empty" << endl;
                    sleep(1);
                    continue;
                }
                cout << qe.i << ":" << qe.s << endl; 
            }
        }
};

int main(int argc, char** argv)
{
    size_t n = 1024*1024*10;
    g_queue.initStore("test.jq", n);
    g_queue.initLock(1019);

    PushThread pt1;
    PushThread pt2;
    pt1.start();
    pt2.start();
    
    PopThread tt1;
    PopThread tt2;
    tt1.start();
    tt2.start();

    pt1.getThreadControl().join();
    
    return 0;
}
